package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.BankListBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.CommentUtil;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AccountActivity extends BaseActivity {


    @BindView(R.id.account_list)
    XRecyclerView accountList;


    private CommonAdapter<BankListBean.DataBean.ObjectsBean> accountAdapter;
    private List<BankListBean.DataBean.ObjectsBean> accountData = new ArrayList<>();
    String TypeValue = "";

    private boolean FistLoad = true;
    private int CurrentNo = 1;


    @Override
    protected void onResume() {
        super.onResume();
        getHttpRequest();
    }

    private void setAppTitle(String title) {

        setTitle("银行卡");

        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

        setActionBtn(R.drawable.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AccountActivity.this, AddAccountActivity.class);
                intent.putExtra("TypeValue", "银行卡账户");
                startActivity(intent);
            }
        });


    }


    void getHttpRequest() {


        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().BANK_CARD_LIST(ConstantsCode.bank_card_list, SharedPreferencesUtil.getID(), "15", CurrentNo + ""), new Response() {
            @Override
            public void OK(Object data) {
                SetNoData(false);
                BankListBean bankListBean = (BankListBean) data;

                if (CurrentNo == 1) {
                    accountData.clear();
                    accountData.addAll(bankListBean.getData().getObjects());
                    accountAdapter.notifyDataSetChanged();
                } else {
                    accountData.addAll(bankListBean.getData().getObjects());
                    accountAdapter.notifyDataSetChanged();
                }


            }

            @Override
            public void Fail(Throwable e) {

            }

            @Override
            public void complete() {

                accountList.loadMoreComplete();

                accountList.refreshComplete();

                if (accountData.size() == 0) {
                    SetNoData(true);
                }
            }
        });


    }


    void setList() {
        accountList.setAdapter(accountAdapter);
        accountList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        accountList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                CurrentNo = 1;
                getHttpRequest();
            }

            @Override
            public void onLoadMore() {
                CurrentNo++;
                getHttpRequest();
            }
        });
    }

    void setAdapter() {

        accountAdapter = new CommonAdapter<BankListBean.DataBean.ObjectsBean>(this, R.layout.item_account, accountData) {
            @Override
            protected void convert(ViewHolder holder, final BankListBean.DataBean.ObjectsBean s, int position) {


                holder.setOnClickListener(R.id.canel_binding, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        CommentUtil.showDialog(mContext, "确认解除绑定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UnBinding(s.getId());
                            }
                        });


                    }
                });

                String endNum = s.getCard();

                //Display the card of bank last 4 num
                if (s.getCard().length() > 4) {
                    endNum = s.getCard().substring(s.getCard().length() - 4, s.getCard().length());
                } else {
                    endNum = s.getCard();
                }
                holder.setText(R.id.tv_my_cash_type, s.getBank())
                        .setText(R.id.bank_card_no, "尾号:" + endNum);


            }
        };

    }

    private void UnBinding(String bankCardNo) {


        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().BANK_CARD_REMOVE(ConstantsCode.bank_card_remove, bankCardNo), new Response() {
            @Override
            public void OK(Object data) {

                getHttpRequest();
            }

            @Override
            public void Fail(Throwable e) {

            }

            @Override
            public void complete() {
                accountList.loadMoreComplete();
                accountList.refreshComplete();
            }
        });
    }


    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_account;
    }


    @Override
    protected void initView() {
        TypeValue = getIntent().getStringExtra("AccountType");


        setAppTitle(TypeValue);

        setAdapter();

        setList();
    }

    @Override
    protected void initNetData() {


    }

    @Override
    protected void customDestroy() {

    }
}
